package classes {
import flash.events.Event;
import flash.utils.getTimer;

import mx.core.Application;

import ws.tink.display.HitTest;
	
	public class Car extends RacingCar  	{
	public var username:String = "player";
	private var totalLaps:int  = 10;
	private var friction:Number = 0.05;
	private var speed:Number = 0;	
	private var rotationStep:Number = 15;
	private var rotationAbility:Number = 0.6;
	private var acceleration:Number = 0.5;	
	private var maxSpeed:int = 5;
	private var backSpeed:int = 1;
	private var currentCheckpoint1:int = 1;
	private var currentLap1:int = 0;
	private var checkpoints:int = 2;
	private var  timer:uint = getTimer();
	
	public var leftKey:Boolean  = false;
	public var upKey:Boolean    = false;
	public var rightKey:Boolean = false;
	public var downKey:Boolean  = false;
	public var spaceKey:Boolean = false;
	public var keyPressed:Boolean = false;		
	public var bestTime:int = 0;
	public var oldSpeedX:Number = 0;
	public var oldSpeedY:Number = 0;
	public var framesElapsedBetween2Synchs:uint = 0;
	
	
	
	private	var checkPoint1Passed:Boolean = false;
	private	var checkPoint2Passed:Boolean = false;
		
	public function Car(){
		this.addEventListener(Event.ENTER_FRAME , enterframe);
	}
	private function testCollision():Boolean {
		var hit:Boolean = HitTest.complexHitTestObject(  Application.application.myuser.myCar,Application.application.track1Limits, 1 );
		return hit;
	}
	
	private function testHasFinishedLoop():Boolean {
		if ((Application.application.track1Limits.checkpoint1.hitTestObject(this))) { 			
			checkPoint1Passed = true;	
		}
		if ((Application.application.track1Limits.checkpoint2.hitTestObject(this))) { 
			checkPoint2Passed = true;checkPoint1Passed=false;		
		}
		return (checkPoint1Passed && checkPoint2Passed);
	}	

	private function enterframe(e:Event):void {
		if (!Game.hasStarted) return;
		// move the opponent !
		framesElapsedBetween2Synchs++;
		if (Application.application.myuser.username!=username) {
			this.x+=oldSpeedX;
			this.y+=oldSpeedY;
			//Application.application.info_txt.text="elpased="+framesElapsedBetween2Synchs+"  oldSpeedX="+oldSpeedX+" oldSpeedY="+oldSpeedY;	
			return;
		}
		// move me !
		
		 var speedX:Number = 0;
		 var speedY:Number = 0;
	
		if (leftKey) {
			this.rotation-=rotationStep;
		}
		if (rightKey) {
			this.rotation+=rotationStep;
		}	
		if (upKey) {
			this.speed+=acceleration;
		}
		if (downKey) {
			this.speed-=acceleration;
		}
		if (speed>maxSpeed) speed=maxSpeed;
		speed-=speed*friction;
		rotationStep = 2+speed /rotationAbility;
		if (testHasFinishedLoop()) {
			Application.application.so_chat.send("gameOver", this.username+" won !");
			return;
		}
		if (testCollision()) {
			speed*=0.55;
		}
		speedX = Math.sin(this.rotation*(Math.PI/180))*this.speed;
		speedY = Math.cos(this.rotation*(Math.PI/180))*this.speed;
		var timeElapsed:uint = getTimer()-timer;
		if (keyPressed || timeElapsed>Game.latency*Game.latencyTolerence) {
			timer = getTimer();
			Application.application.so_chat.send("updateOppentCarPosition",this.username , this.x, this.y , speedX , speedY , this.rotation);
			
		} else {
			
		}
		updateCarPosition(x,y,speedX,speedY);
		
	}
	public function endRace():void {
		this.removeEventListener(Event.ENTER_FRAME , enterframe);
	} 
	
	public function updateCarPosition(x:Number,y:Number,speedX:Number,speedY:Number):void {
		this.x = x;
		this.y = y;
		this.x+=speedX;
		this.y-=speedY;		
	}
		
	public function shoot():void {
		//this.x+=1;
	}

				
		
	}
}